package com.afd.ozblip.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RatingBar;
import android.widget.TextView;

import com.afd.ozblip.activity.R;
import com.afd.ozblip.entity.ReviewEntity;
import com.afd.ozblip.helper.ReviewVH;
import com.afd.ozblip.util.CollectionUtil;
import com.afd.ozblip.util.StringUtil;


public class ReviewListAdapter extends BaseAdapter {

	public List<ReviewEntity> dataList;
	private LayoutInflater inflater;
	private Context context;

	public ReviewListAdapter(Context paramContext) {
		context = paramContext;
		this.dataList = new ArrayList<ReviewEntity>();
		this.inflater = LayoutInflater.from(context);
	}

	/**
	 *获取列表条数
	 */
	@Override
	public int getCount() {
		if (CollectionUtil.UnNullAndZero(this.dataList)) {
			return this.dataList.size();
		} else {
			return 0;
		}

	}

	@Override
	public Object getItem(int paramInt) {
		if (CollectionUtil.UnNullAndZero(this.dataList)) {
			return this.dataList.get(paramInt);
		} else {
			return null;
		}
	}

	@Override
	public long getItemId(int paramInt) {
		return paramInt;
	}

	@Override
	public View getView(int position, View paramView, ViewGroup paramViewGroup) {
		try {
			ReviewVH reviewVH = null;
			if (paramView == null) {
				reviewVH = new ReviewVH();
				paramView = this.inflater.inflate(R.layout.review_list_item,
						null);
				
				reviewVH.userNameTv = (TextView) paramView
						.findViewById(R.id.tv_review_item_userName);
				reviewVH.businessScoreRb = (RatingBar) paramView
						.findViewById(R.id.rb_review_item_star);
				reviewVH.businessPriceTv = (TextView) paramView
						.findViewById(R.id.tv_review_item_price);
				reviewVH.reviewTimeTv = (TextView) paramView
						.findViewById(R.id.tv_review_item_addTime);
				reviewVH.reviewContentTv = (TextView) paramView
						.findViewById(R.id.tv_review_item_content);
				paramView.setTag(reviewVH);
			} else {
				reviewVH = (ReviewVH) paramView.getTag();
			}
			
			ReviewEntity review = this.dataList.get(position);
			if (review != null) {
				// 昵称
				String nickName = review.getUserName();
				if (!StringUtil.isNullOrEmpty(nickName)) {
					reviewVH.userNameTv.setText(nickName);
				}
				// 评分
				Float star = review.getSellerStar();
				if (star!=null) {
					reviewVH.businessScoreRb.setRating(star);
				}
				// 价格
				String price = review.getPrice();
				if (!StringUtil.isNullOrEmpty(price)) {
					reviewVH.businessPriceTv.setText(price);
				}
				// 评价时间
				String time = review.getReviewDate();
				if (!StringUtil.isNullOrEmpty(time)) {
					reviewVH.reviewTimeTv.setText(time);
				}
				// 评价内容
				String content = review.getReviewNaiyo();
				if (!StringUtil.isNullOrEmpty(content)) {
					reviewVH.reviewContentTv.setText(content);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return paramView;
	}

	/**
	 * 添加多项
	 */
	public void addItems(List<ReviewEntity> commentList) {
		this.dataList.addAll(commentList);
	}

	/**
	 * 清除所有项
	 */
	public void removeAllItem() {
		this.dataList.clear();
	}
}

